package com.main.ok.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * @Type JdbcTool
 * @Desc connect to jdbc and run sql
 * @author shuai.zuo
 * @date 2015年2月13日
 * @Version V1.0
 */
public class JdbcTool {

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String url = null;
        String user = null;
        String password = null;
        String sql = null;
        try {
            Class.forName("com.mysql.jdbc.Driver"); // 加载mysq驱动
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载错误");
            e.printStackTrace();// 打印出错详细信息
        }
        try {
            url = "jdbc:mysql://192.168.1.15:3306/push_center?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=10";// 简单写法：url
                                                                                                                                             // =
                                                                                                                                             // "jdbc:myqsl://localhost/test(数据库名)? user=root(用户)&password=yqs2602555(密码)";
            user = "push_center";
            password = "Push_cEN342";
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            System.out.println("数据库链接错误");
            e.printStackTrace();
        }
        try {
            stmt = conn.createStatement();
            sql = "select * from msg_his order by id desc  limit 10 ";// dept这张表有deptno，deptname和age这三个字段
            String sqlinsert="INSERT INTO `msg_his`(gmt_created,gmt_modified,memo,receive_id,content,template_id,type,status,app_id,app_channel,message_id,uid_list) VALUES ('2014-12-22 18:23:47', '2014-12-22 18:23:47', null, null, '{\"appChannelId\":36,\"appId\":\"p_ios_weiyi\",\"appTemplateId\":38,\"context\":\"888\\\\nssssszuoshuai----xssss\",\"dbMsgId\":1071308,\"dbRetryId\":636,\"domainId\":16,\"extParam\":{\"content\":\"ssssszuoshuai----xssss\",\"title\":\"888\"},\"extTemplId\":\"\",\"messageId\":1,\"priority\":1,\"source\":\"FULL\",\"status\":0,\"templateId\":16,\"type\":5}', '16', '5', '0', 'p_ios_weiyi', '1', '1', null);" ;
            rs = stmt.executeQuery(sql);// 执行sql语句
            while (rs.next()) {
                System.out.print(rs.getLong("id") + "   ");
                System.out.println(rs.getString("content")+ "   ");
            }
        } catch (SQLException e) {
            System.out.println("数据操作错误");
            e.printStackTrace();
        }
        // 关闭数据库
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (Exception e) {
            System.out.println("数据库关闭错误");
            e.printStackTrace();
        }
    }
}
